package com.example.demo.Controller;

import com.example.common.redis.RedisCache;
import com.example.common.utils.AjaxResult;
import com.example.demo.domain.DTO.SpotSearch;
import com.example.demo.domain.RecommendInfo;
import com.example.demo.domain.RecommendSearch;
import com.example.demo.domain.SpotInfo;
import com.example.demo.domain.SpotLevelCnt;
import com.example.demo.service.RecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * 推荐业务
 */
@RestController
@RequestMapping("/api/other/recommend")
//@RequestMapping("/recommend")

public class RecommendController {
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private RecommendService recommendService;

    /**
     * 景区搜索栏推荐LEVEL,SPOT_NAME
     *
     * @return 返回18个景区的ID name address provinceName
     */
    @PostMapping("/spotInfoSearch")
    public AjaxResult SpotInfoSearch(String level, String spotName) {
        List<RecommendInfo> spotInfos = recommendService.selectRecommendSpotInfo(level, spotName);
        return AjaxResult.success("查询成功", spotInfos);
    }

    /**
     * 智能景区搜索栏推荐
     *
     * @return 返回18个景区的ID name address provinceName
     */
    @PostMapping("/AISpotInfoSearch")
    @ResponseBody
    public AjaxResult SpotInfoSearch2(@RequestBody RecommendSearch recommendSearch) {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("AISpotInfoSearch"));
        } catch (NullPointerException e) {
            List<RecommendInfo> recommendInfos = recommendService.selectAIRecommendSpotInfo(recommendSearch);
            //设置缓存一天
            redisCache.setCacheJSONObject("AISpotInfoSearch", recommendInfos, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", recommendInfos);
        }
    }
}
